	OUTPUT "vscrtxt.rom"
	;vertical text scroll based on double buffer bitmap
	defpage 1, $4000, $8000 
	

CHRTBL equ 0x0000
NAMTBL equ 0x1800
CLRTBL equ 0x2000
SPRATR equ 0x1B00
FORCLR equ 0xF3E9  ; Foreground color (+1 = background color , +1 border)
CLIKSW equ 0xF3DB  ; bios click switch


	page	1
	
romheader:
	byte "AB"
	word BEGIN
	word 0,0,0,0,0,0

BEGIN:	
	di
	im 		1
	ld 		sp,0xF380	
	ei
	
	
	call 	screen2x16
	call	init_patterns
	call	clean_buffer
	
	ld		hl,text
	ld		[txt_ptr],hl
	ld		a,8
	ld		[row_cntr],a
	
	call	feed_new_line
	/*
	ld		a,2				;frame counter
	push	af	
	*/
	di
main_loop:	
	call	scroll_1_frame
	call	dec_row_cntr
	call	z,feed_new_line
	/*
	pop		af
	dec		a
	jr		z,reset_frame_counter
	jr		reset_frame_counter_end
reset_frame_counter:
	ld		a,2
	halt
	
reset_frame_counter_end:
	push	af
	*/
	//ei
	//halt
	//di

	call	dump_buffer

	jr		main_loop
	
	
	
	jr		$
	
	
	
dump_buffer:
/*
	ld 		hl,txt_buffer
	ld 		de,CHRTBL
	ld 		bc,256*8
	call 	0x005c		;copy to video memory
*/

	xor	a
	ld		hl,CHRTBL

	rlc	h
	rla
	rlc	h
	rla
	srl	h
	srl	h
	di
	out	(#99),a
	ld	a,14+128
	out	(#99),a
	ld	a,l
	nop
	out	(#99),a
	ld	a,h
	or	64
	ei
	out	(#99),a

	ld		b,8
	ld		hl,txt_buffer
	ld		c,0x98
dump_buffer_loop:
	push	bc
	ld		b,0
	otir	
	pop		bc
	djnz	dump_buffer_loop

	ret
	
	
;sets a new line at the end of the buffer
;txt_ptr finish updated
feed_new_line:
	ld		hl,[txt_ptr]
	ld		b,32
	ld		de,txt_buffer + (8*32*8);+ (7*32*8)
	
feed_new_line_loop:	
	push	bc
	push	de

	ld		a,[hl]
	inc		hl
	ld		[txt_ptr],hl
	
	ld		c,32
	sub		c			;a=a-c ;x>32 - 32 = >0 (0..x)
	
	ld		d,0
	ld		e,a
	
	REPEAT	3
			sla		e
			rl		d
	ENDREPEAT
						;de=de*8
	
	ld		hl,font
	add		hl,de		;hl points at font character
						;hl set
						
	pop		de			;de set
	ld		bc,8		;8
	ldir				;character copy
						;de here has the next position to draw in the buffer
	
	pop		bc			;get b counter
	
	ld		hl,[txt_ptr]
	djnz	feed_new_line_loop

	ret
	
	
	
	
scroll_1_frame:
	ld		hl,txt_buffer
	ld		b,(9*8)-1			;b number of times (lines-1)
	ld		a,8

scroll_1_frame_loop:	

	push	bc

	push	af

	push	hl
	call	scroll_1_line
	
	pop		hl
		
	pop		af
	dec		a
	jr		z,scroll_1_frame_loop_renew_a
	jr		scroll_1_frame_loop_one
scroll_1_frame_loop_renew_a:
	ld		a,8
	ld		de,(31*8)+1
	add		hl,de
	//halt
	jr		scroll_1_frame_loop_cont
	
scroll_1_frame_loop_one:
	inc		hl
scroll_1_frame_loop_cont:
	
	pop		bc

	djnz	scroll_1_frame_loop
	
	ret
	

;hl - mem start
;a= row number
scroll_1_line:
	ld		b,32
	;the eighth line has special treatment
	cp		1
	jr		z,scroll_1_line_special

scroll_1_line_normal_loop:
	;normal treatment
	push	bc
	push	hl
	pop		de			;de=hl
	inc		de

	ld		a,[de]
	ld		[hl],a

	ld		b,0
	ld		c,8	
	add		hl,bc
	pop		bc
	djnz	scroll_1_line_normal_loop
	ret

	;special treatment
scroll_1_line_special:
	push	hl

	ld		de,256-8+1
	add		hl,de		;hl src, de dest

	pop		de

scroll_1_line_special_loop:	
	push	bc
		
	ld		a,[hl]
	ld		[de],a
						;let's add 8 to hl and de	
	ld		b,0
	ld		c,8
	ex		de,hl
	add		hl,bc		;adding dest 8
	ex		de,hl
	add		hl,bc		;adding src 8
						;de & hl ready for next loop
	pop		bc
	djnz	scroll_1_line_special_loop	

	ret
	

;Zero flag if set and [row_cntr] updated
dec_row_cntr:
	ld		a,[row_cntr]
	dec		a
	jr		z,dec_row_cntr_restart
	jr		dec_row_cntr_set
dec_row_cntr_restart:
	ld		a,8
dec_row_cntr_set:
	ld		[row_cntr],a	
	ret
	
	
clean_buffer:
	ld		hl,txt_buffer
	xor		a
	ld		[hl],a
	ld		de,txt_buffer+1
	ld		bc,(9*32*8)-1
	ldir
	ret
	

init_patterns:
	ld		a,0
	ld		bc,3*256*8			;clean all character patterns
	ld		hl,CHRTBL
	call	0x56


	ld		b,0
	xor		a
	ld		hl,txt_buffer
init_patterns_loop:
	ld		[hl],a
	inc		a
	inc		hl
	djnz	init_patterns_loop
	
	
	ld 		hl,txt_buffer
	ld 		de,NAMTBL
	ld 		bc,256 
	call 	0x005c		;copy to video memory
	
	
;colors
	ld		a,0xF1
	ld		bc,3*256*8			;set all character colors
	ld		hl,CLRTBL
	call	0x56

	ret



	
	

screen2x16:
;<<inicialisacion>>
; Set background and border
	ld      hl,FORCLR
	ld      [hl],15
	inc     hl
	ld      [hl],1
	inc		hl
	ld		[hl],1
	call    0x062

;click off	
	xor	a		
	ld	[CLIKSW],a
		
;- screen 2
	ld a,2
	call 0x005f

	ld b,0xe2
;ld b,0xe3 
	ld c,1
	call 0x47

	ret



cls_screen:
	ld		a,0
	ld		bc,3*256
	ld		hl,NAMTBL
	call	0x56

	ret


cls_sprites:
	ld		a,0
	ld		bc,4*64
	ld		hl,SPRATR
	call	0x56

	ret
	
	
	

text:
		db	"MSX IS THE NAME OF A STANDARDIZED HOME COMPUTER ARCHITECTURE, FIRST ANNOUNCED BY MICROSOFT ON JUNE 16, 1983,[1] CONCEIVED BY KAZUHIKO NISHI, THEN VICE-PRESIDENT AT MICROSOFT JAPAN AND DIRECTOR AT ASCII CORPORATION. IT IS SAID THAT MICROSOFT LED THE PROJECT AS AN ATTEMPT TO CREATE UNIFIED STANDARDS AMONG HARDWARE MAKERS.[2]"
		db	"DESPITE MICROSOFT'S INVOLVEMENT, THE MSX-BASED MACHINES WERE SELDOM RELEASED IN THE UNITED STATES,[3] BUT WERE POPULAR MOSTLY IN JAPAN, THE MIDDLE EAST, BRAZIL,[CITATION NEEDED] THE SOVIET UNION, THE NETHERLANDS,[CITATION NEEDED] SPAIN, AND TO A LESSER EXTENT, SEVERAL OTHER EUROPEAN COUNTRIES. IT IS DIFFICULT TO ESTIMATE HOW MANY MSX COMPUTERS WERE SOLD WORLDWIDE, BUT EVENTUALLY 5 MILLION MSX-BASED UNITS WERE SOLD IN JAPAN ALONE.[CITATION NEEDED]"
		db	"BEFORE THE APPEARANCE AND GREAT SUCCESS OF NINTENDO'S FAMILY COMPUTER, MSX WAS THE PLATFORM FOR WHICH MAJOR JAPANESE GAME STUDIOS, SUCH AS KONAMI AND HUDSON SOFT, PRODUCED VIDEO GAME TITLES. THE METAL GEAR SERIES, FOR EXAMPLE, WAS ORIGINALLY WRITTEN FOR MSX HARDWARE.."
		db  "IN THE 1980S, JAPAN WAS IN THE MIDST OF AN ECONOMIC AWAKENING.[5] LARGE JAPANESE ELECTRONICS FIRMS MIGHT HAVE BEEN SUCCESSFUL IN THE EARLY COMPUTER MARKET HAD THEY MADE A CONCERTED EFFORT IN THE LATE 1970S. THEIR COMBINED DESIGN AND MANUFACTURING POWER COULD HAVE ALLOWED THEM TO PRODUCE COMPETITIVE MACHINES, BUT THEY INITIALLY IGNORED THE HOME COMPUTER MARKET AND APPEARED HESITANT TO DO BUSINESS IN A MARKET WHERE NO INDUSTRY STANDARD EXISTED.[6]"
		db	"NISHI PROPOSED MSX AS AN ATTEMPT TO CREATE A SINGLE INDUSTRY STANDARD FOR HOME COMPUTERS. INSPIRED BY THE SUCCESS OF VHS AS A STANDARD FOR VIDEO CASSETTE RECORDERS, MANY JAPANESE ELECTRONIC MANUFACTURERS ALONG WITH GOLDSTAR, PHILIPS AND SPECTRAVIDEO BUILT AND PROMOTED MSX COMPUTERS. ANY PIECE OF HARDWARE OR SOFTWARE WITH THE MSX LOGO ON IT WAS COMPATIBLE WITH MSX PRODUCTS OF OTHER MANUFACTURERS. IN PARTICULAR, THE EXPANSION CARTRIDGE FORM AND FUNCTION WERE PART OF THE STANDARD; ANY MSX EXPANSION OR GAME CARTRIDGE WOULD WORK IN ANY MSX COMPUTER."
		db	"NISHI'S STANDARD WAS BUILT AROUND THE SPECTRAVIDEO SV-328 COMPUTER.[7] THE STANDARD CONSISTED PRIMARILY OF SEVERAL OFF-THE-SHELF PARTS; THE MAIN CPU WAS A 3.58 MHZ ZILOG Z80,[8] THE GRAPHICS CHIP A TEXAS INSTRUMENTS TMS9918 WITH 16 KB OF DEDICATED VRAM, THE SOUND AND PARTIAL I/O SUPPORT WAS PROVIDED BY THE AY-3-8910 CHIP MANUFACTURED BY GENERAL INSTRUMENT (GI), AND AN INTEL 8255 PROGRAMMABLE PERIPHERAL INTERFACE CHIP WAS USED FOR THE PARALLEL I/O SUCH AS THE KEYBOARD. THIS WAS A CHOICE OF COMPONENTS THAT WAS SHARED BY MANY OTHER HOME COMPUTERS AND"
		db	"GAMES CONSOLES OF THE PERIOD, SUCH AS THE COLECOVISION HOME COMPUTER (AN EMULATOR WAS LATER AVAILABLE WITH WHICH MSX SYSTEMS COULD RUN SOME OF ITS SOFTWARE), AND THE SEGA SG-1000 VIDEO GAME SYSTEM. TO REDUCE OVERALL SYSTEM COST, MANY MSX MODELS USED A CUSTOM IC KNOWN AS 'MSX-ENGINE', WHICH INTEGRATED GLUE LOGIC, 8255 PPI, YM2149 COMPATIBLE SOUNDCHIP AND MORE, SOMETIMES EVEN THE Z80 CPU. HOWEVER, ALMOST ALL MSX SYSTEMS USED A PROFESSIONAL KEYBOARD INSTEAD OF A CHICLET KEYBOARD, DRIVING THE PRICE UP AGAIN. CONSEQUENTLY, THESE COMPONENTS ALONGSIDE MICROSOFT'S MSX BASIC MADE THE MSX A COMPETITIVE, THOUGH SOMEWHAT EXPENSIVE, HOME COMPUTER PACKAGE."
endtext:
		db	0
		
		
font:
	;32 , blank
	DB $00, $00, $00, $00, $00, $00, $00, $00

	DB $00, $ef, $ef, $ef, $00, $7f, $7f, $7f
	DB $00, $ef, $ef, $ef, $00, $7f, $7f, $7f
	DB $f0, $f0, $f0, $f1, $f1, $f1, $ff, $ff
	DB $f8, $7b, $3b, $1d, $0e, $06, $02, $00
	DB $00, $00, $00, $e3, $e3, $e3, $ff, $ff
	DB $00, $00, $00, $c7, $c7, $c7, $ff, $ff
	DB $b8, $78, $96, $76, $ec, $98, $fc, $6c
	DB $0a, $0f, $07, $06, $05, $02, $07, $07
	DB $1d, $1e, $69, $6e, $37, $19, $3f, $36
	DB $50, $f0, $e0, $60, $a0, $40, $e0, $e0

	DB $00,$00,$18,$18,$7E,$18,$18,$00 ;43
	DB $00,$00,$00,$00,$00,$18,$30,$00,$00,$00,$00,$00,$7E,$00,$00,$00 
	DB $00,$00,$00,$00,$00,$00,$18,$00,$00,$00,$0C,$18,$30,$60,$C0,$00
	DB $00,$00,$7C,$CE,$D6,$E6,$7C,$00,$00,$00,$18,$38,$18,$18,$7E,$00
	DB $00,$00,$7C,$06,$7E,$60,$7E,$00,$00,$00,$7C,$06,$7C,$06,$7C,$00
	DB $00,$00,$CC,$CC,$FE,$0C,$0C,$00,$00,$00,$7E,$60,$7C,$06,$7C,$00
	DB $00,$00,$3E,$60,$7C,$66,$3C,$00,$00,$00,$FE,$06,$0C,$18,$30,$00
	DB $00,$00,$3C,$66,$3C,$66,$3C,$00,$00,$00,$3C,$66,$3C,$06,$7C,$00
	DB $00,$00,$00,$18,$00,$18,$00,$00,$00,$00,$00,$18,$00,$18,$30,$0E
	DB $18,$30,$60,$30,$18,$0E,$00,$00,$00,$00,$00,$7E,$00,$7E,$00,$00
	DB $18,$0C,$06,$0C,$18,$70,$00,$00,$00,$00,$38,$6C,$08,$00,$18,$00
	DB $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$7C,$C6,$FE,$C6,$C6,$00
	DB $00,$00,$FC,$C6,$FC,$C6,$FC,$00,$00,$00,$7C,$C6,$C0,$C6,$7C,$00
	DB $00,$00,$FC,$C6,$C6,$C6,$FC,$00,$00,$00,$FE,$C0,$FE,$C0,$FE,$00
	DB $00,$00,$FE,$C0,$FE,$C0,$C0,$00,$00,$00,$7E,$C0,$DC,$C6,$7C,$00
	DB $00,$00,$C6,$C6,$FE,$C6,$C6,$00,$00,$00,$FC,$30,$30,$30,$FC,$00
	DB $00,$00,$FE,$06,$06,$C6,$7C,$00,$00,$00,$C6,$CC,$F8,$CC,$C6,$00
	DB $00,$00,$C0,$C0,$C0,$C0,$FE,$00,$00,$00,$C6,$EE,$FE,$D6,$C6,$00
	DB $00,$00,$C6,$E6,$FE,$CE,$C6,$00,$00,$00,$7C,$C6,$C6,$C6,$7C,$00
	DB $00,$00,$FC,$C6,$FC,$C0,$C0,$00,$00,$00,$7C,$C6,$C6,$CC,$76,$00
	DB $00,$00,$FC,$C6,$FC,$CC,$C6,$00,$00,$00,$7E,$C0,$7C,$06,$FC,$00
	DB $00,$00,$FC,$30,$30,$30,$30,$00,$00,$00,$C6,$C6,$C6,$C6,$7C,$00
	DB $00,$00,$C6,$C6,$C6,$6C,$38,$00,$00,$00,$C6,$D6,$FE,$EE,$C6,$00
	DB $00,$00,$C6,$6C,$38,$6C,$C6,$00,$00,$00,$C6,$C6,$7E,$06,$FC,$00
	DB $00,$00,$FC,$18,$30,$60,$FC,$00			;90->Z

		
		
	org	0xc000		
		
txt_ptr:		ds	2			;text-endtext
row_cntr:		ds	1			;0-7
txt_buffer		ds	8*32*8
